package com.changgou.business.controller;
import com.changgou.business.pojo.AConfig;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.business.service.AdService;
import com.changgou.business.pojo.Ad;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.OutputKeys;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/ad")
public class AdController {


    @Autowired
    private AdService adService;

    /**
     * 添加广告
     * @param ad
     * @return
     */
    @PostMapping
    public Result add(@PathVariable Ad ad){
        adService.add(ad);
        return new Result(true, StatusCode.OK,"添加广告成功");
    }


    /**
     * 设置广告置顶
     * @return
     */
    @GetMapping("/updateTop/{id}/{position}")
    public Result updateTop(@PathVariable(value = "id") int id , @PathVariable(value = "position") int position){
        adService.updateTop(id,position);
        return new Result(true,StatusCode.OK,"置顶设置成功");
    }

    /**
     * 广告分页查询条件
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/findPage/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap,@PathVariable("page")int page,@PathVariable("size") int size){
        Page pageInfo = adService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 广告删除
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id){
        adService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }







}
